package com.study.jdbc.pool;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.study.jdbc.utils.DBUtil;

/**
* @Name: SimpleConnectionPool
* @Description: 数据库连接池简单模拟类
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-2-18 上午10:25:04 
* @Version: V1.0
 */
public class SimpleConnectionPool {
	
	private static List<Connection> pool = new ArrayList<Connection>() ;
	
	static {
		//初始化10个连接到池中
		for (int i = 0; i < 10; i++) {
			Connection con = DBUtil.getConnection() ;
			pool.add(con) ;
		}
	}
	
	public synchronized static Connection getConnection() {
		if(pool.size() > 0) {
			Connection con = pool.remove(0) ;
			return con ;
		} else {
			throw new RuntimeException("对不起，服务器繁忙！") ;
		}
	}
	
	public static void release(Connection con) {
		pool.add(con) ;
	}

}
